package com.example.administrator.shzs.db;

import android.content.Context;
import android.util.Log;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 *  操作数据库
 */
public class OperateGoodsDataBaseStatic{


    /**
     *添加和删除商品数量
     */
    public static int saveGoodsNumber(Context context, int shopid, int goodsid, String goodsname, String goodsnum, String goodsprice, String userid) {
        DbUtils utils = DbUtils.create(context);
        GoodsBean goodsBean = null;
        goodsBean =new GoodsBean();
        //保存到数据库
        goodsBean.setShopid(shopid);//商家Id
        goodsBean.setGoodsId(goodsid);//商品Id
        goodsBean.setGoodsName(goodsname);//商品名
        goodsBean.setGoodsNum(goodsnum);//商品数量
        goodsBean.setGoodsPrice(goodsprice);//商品价格
        goodsBean.setUserid(userid);//用户唯一标识userid
        try {
            //通过商家Id和商品Id查找或保存
            GoodsBean bean = utils.findFirst(Selector.from(GoodsBean.class).where("shopid" , "=" , shopid).and("goodsid", "=", goodsid));
            //如果有这条数据，数量直接加1；否则就插入表里面
            if(bean == null){
                Log.e("TAG", "还没有该商品");
                utils.save(goodsBean);
                Log.e("TAG" , "该商品已经存储");
                return getGoodsNumber(context , shopid , goodsid);
            }else{
                Log.e("TAG" , "已经有该商品");
                //返回添加商品之后的商品总数
                return updateNum(context, shopid, goodsid, goodsnum);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        Log.e("TAG" , "添加商品失败");
        utils.close();
        return 0;
    }




    /**
     * 通过<<<商家Id和商品Id字段>>>查找
     * 修改数量，直接传入数量
     */
    public static int updateNum(Context context , int shopid,int goodsid , String goodsnum){
        DbUtils utils = DbUtils.create(context);
        try {
            //修改数量
            GoodsBean bean = utils.findFirst(Selector.from(GoodsBean.class).where("shopid", "=", shopid).and("goodsid", "=", goodsid));
            bean.setGoodsNum(goodsnum);
            utils.update(bean);
            Log.e("TAG", "该商品数量改变为：" + getGoodsNumber(context, shopid, goodsid));
            return getGoodsNumber(context , shopid , goodsid);
        } catch (DbException e) {
            e.printStackTrace();
        }
        utils.close();
        return 0;
    }




    /**
     *根据<<<商家Id和商品Id字段>>>获取
     * 当前分类的购物数量
     */
    public static int getGoodsNumber(Context context ,int shopid, int goodsid) {
        DbUtils utils = DbUtils.create(context);
        if(utils == null){
            Log.e("TAG" , "还没有该数据库");
            return 0;
        }

        try {
            GoodsBean bean = utils.findFirst(Selector.from(GoodsBean.class).where("shopid", "=", shopid).and("goodsid", "=", goodsid));


            if(bean == null){
                Log.e("TAG" , "还没有该存储商品");
                return 0;
            }else{
                Log.e("TAG" , "根据商家Id和商品Id   获取商品数量成功:" + Integer.parseInt(bean.getGoodsNum().trim()));
                return Integer.parseInt(bean.getGoodsNum());
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        utils.close();
        Log.e("TAG", "根据商家Id和商品Id   获取商品数量失败");
        return 0;
    }




    /**
     * 通过<<<商品Id字段>>>查找数据库中所有商品数量
     */
    public static int getGoodsNumberAll(Context context, int shopid) {
        DbUtils utils = DbUtils.create(context);
        int mGoodsNum = 0;

        //获取数据库所有数据
        ArrayList<GoodsBean> mGoodsBeanList = null;
        mGoodsBeanList = getGoodsTypeList(context);
        if(mGoodsBeanList == null){
            Log.e("TAG" , "获取商品类型总数失败");
            return 0;
        }

        for(int i = 0 ; i < mGoodsBeanList.size() ; i++){

            //通过商品goodsid取出所有商品数量
            if(mGoodsBeanList.get(i).getShopid() == shopid){
                mGoodsNum += Integer.parseInt(mGoodsBeanList.get(i).getGoodsNum().trim());
            }
        }
        Log.e("TAG", "根据商品Id  得到所有购物数量成功：" + mGoodsNum);
        utils.close();
        return mGoodsNum;
    }




    /**
     *获取所有商品数据
     */
    public static ArrayList<GoodsBean> getGoodsTypeList(Context context){
        DbUtils utils = DbUtils.create(context);
        ArrayList<GoodsBean> list = null;
        try {
            //查询数据库所有数据
            list = (ArrayList<GoodsBean>)utils.findAll(GoodsBean.class);


            if(list == null){
                Log.e("TAG" , "商品还没有存储数据");
                return null;
            }else{
                Log.e("TAG" , "根据商品Id 得到购物车中所有商品类型总数成功： 数量为:" + list.size());
                utils.close();
                return list;
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        Log.e("TAG" , "根据商家Id和商品Id 得到商品类型总数失败");

        return null;
    }




    /**
     *根据<<<商品Id字段>>>获取所有购物的价格
     */
    public static double getGoodsPriceAll(Context context, int shopid) {
        DbUtils utils = DbUtils.create(context);
        double mGoodsPrice = 0;
        ArrayList<GoodsBean> mGoodsBeanList = null;
        mGoodsBeanList = getGoodsTypeList(context);
        if(mGoodsBeanList == null){
            Log.e("TAG" , "获取商品类型总数失败");
            return 0;
        }
        for(int i = 0 ; i < mGoodsBeanList.size(); i++){
            if(mGoodsBeanList.get(i).getShopid() == shopid){
                mGoodsPrice += (Integer.parseInt(mGoodsBeanList.get(i).getGoodsNum().trim())) * (Double.parseDouble(mGoodsBeanList.get(i).getGoodsPrice().trim()));
                Log.e("TAG" , "根据商品Id 得到所有购物的价格成功：" + mGoodsPrice);
            }
        }
        utils.close();
        return mGoodsPrice;
    }


    /**
     * 删除单条数据<<传入 商品goodsId>>
     * @param context
     * @param goodsid
     */
    public static void deleteSingle(Context context,String num,int goodsid){
        //数量为零时删除这条商品数据
        DbUtils dbUtils = DbUtils.create(context);
        try {
            dbUtils.delete(GoodsBean.class, WhereBuilder.b("goodsid","=",goodsid));
            Log.e("TAG","数量为"+num+",   删除成功！ 商品Id=="+goodsid);
        } catch (DbException e) {
            e.printStackTrace();
        }

        dbUtils.close();
    }


    /**
     *删除所有的购物数据
     */
    public static void deleteAll(Context context) {
        DbUtils utils = DbUtils.create(context);

        try {
            List<GoodsBean> records = utils.findAll(GoodsBean.class);
            utils.deleteAll(records);
        } catch (DbException e) {
            e.printStackTrace();
        }
        utils.close();
    }


    /**
     * 根据商家shopId获取购物车所有商品数据
     * @param context
     * @param shopid 商家Id
     * @return
     */
    public static List<GoodsBean> getGoodsCarData(Context context,int shopid){

        DbUtils dbUtils=DbUtils.create(context);

        List<GoodsBean> goodsBeans=null;

        try {

            goodsBeans= dbUtils.findAll(Selector.from(GoodsBean.class).where(WhereBuilder.b("shopid", "=", shopid)));

            Log.e("TAG","根据商家shopId获取所有商品数据成功。"+goodsBeans.size());
            return goodsBeans;
        } catch (DbException e) {
            e.printStackTrace();
        }

        dbUtils.close();
        Log.e("TAG","根据商家shopId获取所有商品数据失败！！。");
        return null;
    }

}
